package com.flymobi.mshop.activity;

import java.util.List;
import java.util.Map;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.flymobi.mshop.R;
import com.flymobi.mshop.adapter.ClassificationAdapter;
import com.flymobi.mshop.util.Constants;
import com.flymobi.mshop.util.service.ProductService;

/**
 * 
 * @author 余鹏 分类列表详情页面
 */

public class ClassificationBrowse extends CommonActivity implements
		OnClickListener {
	private TextView datas;

	private ListView loadClass;

	private ProgressDialog mydialog;

	private List<Map<String, Object>> prodList;

	private String id;

	private TextView txtNew;
	private TextView pinNum;
	private TextView price;

	//                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
	// private TextView prices;
	// private TextView market;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.brows);
		// MyApplication.getInstance().addActivity(this);
		// 初始化控件
		txtNew = (TextView) findViewById(R.id.txtNews);
		pinNum = (TextView) findViewById(R.id.pinNum);
		price = (TextView) findViewById(R.id.price);
		datas = (TextView) findViewById(R.id.datas);
		// 为控件绑定事件
		txtNew.setOnClickListener(this);
		pinNum.setOnClickListener(this);
		price.setOnClickListener(this);
		// 调用list方法，参数自定义一个常量，用于排序。
		list(Constants.ProdRank.time);

		// 接受传过来的值
		id = (String) (this.getIntent().getExtras().getString("id"));
		String name = (String) (this.getIntent().getExtras().getString("name"));
		// datas的文本显示为name
		datas.setText(name);
		// 按钮的onclick方法
	}

	// 创建一个handler对象
	Handler hdl = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				mydialog.dismiss();
				ClassificationAdapter classIfcation = new ClassificationAdapter(
						ClassificationBrowse.this, prodList);
				loadClass.setAdapter(classIfcation);
			}
			return;
		};
	};

	// 加载分类详情列表
	public void list(final String rank) {
		// 初始化ListView控件
		loadClass = (ListView) findViewById(R.id.loadClass);
		// 打开一个模式化进度条对话框
		mydialog = ProgressDialog.show(ClassificationBrowse.this, null,
				"数据加载中...", true);
		// 新建一个线程
		new Thread() {
			public void run() {
				try {
					// 调用后台的分类详细查询方法。
					prodList = ProductService.list(id, rank);
					Message msg = new Message();
					msg.what = 1;
					hdl.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}

	// 吧几个按钮做成选项卡的效果
	public void setTextBack(View v) {
		txtNew.setBackgroundResource(R.drawable.android_sort_left);
		pinNum.setBackgroundResource(R.drawable.android_sort_left);
		price.setBackgroundResource(R.drawable.android_sort_left);
		v.setBackgroundResource(R.drawable.android_sort_left_pressed);
		String rank = "";
		switch (v.getId()) {
		case R.id.txtNews:
			rank = Constants.ProdRank.time;
			break;
		case R.id.pinNum:
			rank = Constants.ProdRank.sale;
			break;
		case R.id.price:
			rank = Constants.ProdRank.price;
			break;
		}
		list(rank);
	}

	// 按钮的单击事件
	@Override
	public void onClick(View v) {
		setTextBack(v);
	}
}
